package com.gqqx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gqqx.common.exception.CustomException;
import com.gqqx.common.result.Rt;
import com.gqqx.user.mapper.UserMapper;
import com.gqqx.user.pojo.User;
import com.gqqx.user.service.UserService;
import com.gqqx.user.utils.UserHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.util.Objects;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

//    @Autowired
//    private RedisTemplate redisTemplate;

    @Override
    public Rt userLogin( HttpSession session,String name , String password) {
        //将页面提交的密码password进行md5加密处理
        if (Objects.isNull(name)){
            throw new CustomException("用户名为空");
        }
        if (Objects.isNull(password)) {

            throw new CustomException("密码为空");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,name)
                .eq(User::getPassword,password);
        User user = baseMapper.selectOne(queryWrapper);
        if (user!=null){
            session.setAttribute("user",user);
//            System.out.println(request.getSession().getAttribute("id"));
//            redisTemplate.opsForValue().set(RedisPrefix.ID_KEY + user.getId(),user.getId());
            return Rt.success(user.getId());
        }else {
            return Rt.error();
        }
    }
}
